﻿using BLL;
using DAL.DBInteractions;
using GUI.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.SessionState;

namespace GUI.Infrastructure
{
    public class CustomControllerFactory : IControllerFactory
    {
        private readonly IUnitOfWork unitofwork;

        private IDBFactory dbf = new DBFactory();
        public CustomControllerFactory()
        {

            unitofwork = (IUnitOfWork)new UnitOfWork(dbf);
        }



        public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
        {

            if (controllerName.ToLower().StartsWith("auteur"))
            {


                var service = new AuteurServices(unitofwork);
                var controller = new AuteurController(service);
                return controller;
            }

            var defaultFactory = new DefaultControllerFactory();
            return defaultFactory.CreateController(requestContext, controllerName);

        }

        public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(System.Web.Routing.RequestContext requestContext, string controllerName)
        {
            return SessionStateBehavior.Default;
        }

        public void ReleaseController(IController controller)
        {
            var dispoable = controller as IDisposable;
            if (dispoable != null)
            {
                dispoable.Dispose();
            }
        }
    }
}